//
//  WmcMediaWatermarkAPI.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/9.
//

import Foundation
import YPNetwork

struct WmcMediaWatermarkAPI {
    /// 水印模板列表
    /// - DocumentUrl: http://yapi.3pvr.com/project/698/interface/api/10731
    /// - Parameters:
    ///   - classifyIds: 分类id
    ///   - watermarkIds: 水印id
    ///   - success: 水印列表
    ///   - failure: 请求失败
    static func fetchWatermarkList(classifyIds: [Int]? = nil,
                                   watermarkIds: [Int]? = nil,
                                   success: (([WmcWatermarkClassifyModel]) -> Void)? = nil,
                                   failure: YPNetworkRequestFailure? = nil)
    {
        if let watermarkIds = watermarkIds { // 有查询
            var params: [String: Any] = [:]
            params["watermarkIds"] = watermarkIds
            WmcMediaRequest.requestData(path: "/media/v1/watermark/query", parameters: params) { request in
                request.method = .post
            } success: { (response: YPNetworkResponse) in
                if let dic = response.data as? [String: Any],
                   let list = dic["watermarkList"] as? [Any]
                {
                    let classify = WmcWatermarkClassifyModel()
                    let resu = list.parseListModelWithClass(WmcWatermarkDetailModel.self)

                    resu.forEach { detailModel in
                        detailModel.fields.forEach { optionModel in
                            if optionModel.fieldType == .single {
                                optionModel.options = optionModel.content.yp.split(separator: ",")
                                if optionModel.options.count > 0 {
                                    optionModel.content = optionModel.options[0]
                                }
                            }
                        }
                    }

                    classify.watermarks = resu
                    success?([classify])
                } else {
                    failure?(.init(code: 0, message: "参数解析失败！"))
                }
            } failure: { err in
                failure?(err)
            }
        } else {
            var params: [String: Any] = [:]
            if let classifyIds = classifyIds, !classifyIds.isEmpty {
                params = ["classifyIds": classifyIds]
            }
            if let watermarkIds = watermarkIds, !watermarkIds.isEmpty {
                params = ["watermarkIds": watermarkIds]
            }
            WmcMediaRequest.requestData(path: "/media/v2/watermark/list", parameters: params) { request in
                request.method = .post
            } success: { (response: YPNetworkResponse) in
                if let dic = response.data as? [String: Any],
                   let list = dic["list"] as? [Any]
                {
                    let resu = list.parseListModelWithClass(WmcWatermarkClassifyModel.self)
                    success?(resu)
                } else {
                    failure?(.init(code: 0, message: "参数解析失败！"))
                }
            } failure: { err in
                failure?(err)
            }
        }
    }

    /// 查询企业logo
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511880&itf=2269144
    /// - Parameters:
    ///   - logo: 搜索关键字
    ///   - page: 当前页，从1开始
    ///   - pageSize: 页大小，最小10，最大50
    /// - Returns: Observable<WmcMediaEnterpriseLogoModel>
    static func fetchEnterpriseLogo(_ name: String? = nil, page: Int, pageSize: Int) -> Observable<WmcMediaEnterpriseLogoModel> {
        var params: [String: Any] = ["currentPage": page, "pageSize": pageSize]
        if let name = name {
            params["name"] = name
        }
        return WmcMediaRequest.requestData(path: "/media/v1/brand/search", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaEnterpriseLogoModel.self)
    }

    /// 水印分享wx接口
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511880&itf=2286445
    /// - Parameters:
    ///   - watermarkInfo: 水印模板信息json
    ///   - watermarkUrl: 水印模板图片url
    /// - Returns: Observable<WmcMediaWatermarkShareModel>
    static func fetchWatermarkShare(watermarkInfo: [String: Any], watermarkUrl: String) -> Observable<WmcMediaWatermarkShareModel> {
        let params: [String: Any] = ["wmcInfo": watermarkInfo, "wmUrl": watermarkUrl]
        return WmcMediaRequest.requestData(path: "/media/v1/watermark/share", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaWatermarkShareModel.self)
    }

    /// 获取分享水印模板
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511880&itf=2286456
    /// - Parameter code: code
    /// - Returns: Observable<wmcMediaWatermarkShareTemplate>
    static func fetchWatermarkTemplate(code: String) -> Observable<WmcMediaWatermarkShareTemplate> {
        let params: [String: Any] = ["code": code]
        return WmcMediaRequest.requestData(path: "/media/v1/watermark/getWmInfo", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaWatermarkShareTemplate.self)
    }
}
